import Vue from 'vue'
import Vuex from 'vuex'
// 引入BMap
import BMap from 'BMap'
Vue.use(Vuex)

// 定义获取定位方法
function getCity () {
  var geolocation = new BMap.Geolocation()
  geolocation.getCurrentPosition(function (r) {
    const myGeo = new BMap.Geocoder()
    myGeo.getLocation(new BMap.Point(r.point.lng, r.point.lat), data => {
      // console.log(data.addressComponents)
      // console.log(data.addressComponents.city.replace('市', ''))
      // 写入本地存储
      localStorage.city = data.addressComponents.city.replace('市', '')
      // localStorage.setItem('city', data.addressComponents.city.replace('市', ''))
    })
  })
}
getCity()

let state = {
  // city: localStorage.getItem('city') ? localStorage.getItem('city') : '西安'
  // city: localStorage.getItem('city')
  // city: localStorage.getItem('city')
  // 从本地存储中获取数据
  city: localStorage.city ? localStorage.city : '北京'
}
let mutations = {
  changeCity (state, city) {
    // this.state.city = 'aaa'
    // console.log(city)
    this.state.city = city
    localStorage.setItem('city', city)
  }
}
export default new Vuex.Store({
  state,
  mutations
})
